<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:attachment:export']"
        >下载</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="attachmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">{{ (scope.$index + 1) }}</template>
      </el-table-column>
      <el-table-column label="附件名称" align="center" prop="attachmentName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="viewAttachments(scope.row.id)"
            v-hasPermi="['system:attachment:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="success"
            icon="el-icon-delete"
            @click="handleExport(scope.row)"
            v-hasPermi="['system:attachment:remove']"
          >下载</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {
  listAttachment,
  getAttachment,
  delAttachment,
  addAttachment,
  updateAttachment
} from "@/api/system/attachment";

export default {
  name: "Attachment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 证件信息附件表格数据
      attachmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        certificateDocumentId: null,
        attach: null,
        createUser: null,
        createDate: null,
        updateUser: null,
        updateDate: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询证件信息附件列表 */
    getList() {
      this.loading = true;
      listAttachment(this.queryParams).then(response => {
        this.attachmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        certificateDocumentId: null,
        attach: null,
        remark: null,
        createUser: null,
        createDate: null,
        updateUser: null,
        updateDate: null,
        status: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加证件信息附件";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAttachment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAttachment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/attachment/export",
        {
          ...this.queryParams
        },
        `attachment_${new Date().getTime()}.xlsx`
      );
    },
    viewAttachments(id) {
      getAttachment(id)
        .then(response => {
          this.attachments = response.rows;
          this.dialogVisible = true;
        })
        .catch(error => {
          console.error("Error fetching attachments:", error);
        });
    }
  }
};
</script>